package com.dly.hope.repository;

import com.dly.hope.workflow.NodeInstance;
import com.dly.hope.workflow.dto.ApprRecordDTO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

/**
 * @Name: NodeInstanceRepository
 * @Description:
 * @Author: Danny Chen
 * @Date: 2022/1/18
 */
public interface NodeInstanceRepository extends JpaRepository<NodeInstance, String> {

//    List<ApprRecordDTO> getApprRecordDtoByProcesInstanceId(String processInstanceId, Integer status, Integer notEndFlag, Integer displayFlag);
//
//    @Query("select new com.dragonsoftbravo.doa.domain.workflow.dto.ApprCountDTO(pi.category, pi.name, count(ni.id)) from NodeInstance ni " +
//            "inner join ni.nodeInfo nif inner join nif.processInfo pi where ni.dataStatus = ?2 and ni.completeFlag = ?3 " +
//            "and exists (select nr from NodeRole nr where nr.roleId = ?1 and nr.nodeId = ni.nodeId) group by pi.category, pi.name " +
//            "order by pi.category, pi.name")
//    List<ApprCountDTO> getCountByRoleId(String roleId, Integer status, Integer completeFlag);

    Optional<NodeInstance> findByIdAndDataStatus(String id, Integer dataStatus);

    Optional<NodeInstance> findTopByProcessInstanceIdAndDataStatus(String processId, Integer dataStatus);

    Optional<NodeInstance> findTopByProcessInstanceIdAndDataStatusOrderByCreateDateDesc(String processId, Integer dataStatus);

    List<NodeInstance> findAllByProcessInstanceIdInAndDataStatus(List<String> instanceIds, Integer dataStatus);

    List<NodeInstance> findAllByIdInAndDataStatus(List<String> ids, Integer dataStatus);

//    @Query("select emp.name from NodeInstance ni inner join UserInfo usr on ni.approverId = usr.id " +
//            "inner join EmpInfo emp on usr.employeeId = emp.id " +
//            "inner join RoleInfo rl on usr.roleId = rl.id where ni.processInstanceId = ?1 and rl.name = ?2 and ni.dataStatus = ?3 and ni.apprResult = 'Y' order by ni.createDate desc")
//    List<String> getApproverByProcessInstanceIdAndRoleName(String processInstanceId, String roleName, Integer status);


}
